//Wait for page to be loaded to start script
window.onload = main;
var gameLoop;

var gameover = false;

//Player start position
var playerCar;
var PLAYER_START_X = 330;
var PLAYER_START_Y = 400;

//mousewheel data
var delta = 0;

//Gameplay Variables 
var TimeLeft = 60;
var SpeedSum = 0;
var Distance = 0.01;

var FRICTION_RATE = 40;

//Sprites 
var player_img_array = [];
var road_img_array = [];

var animationCounter = 0;
var SPEED_CALC_INTERVAL = 5; //number of gameloops for each speed calculation

//Speed Variables
var ScrollSpeed = 0;
var Speed = 0;

//Varibles from algorithm
var ROADLINES = 150;
var TEXSIZE = 80;
var RoadY = -1;        //arbitrary
var ResX = 800;
var ResY = 350;
var PlrLine = 8;       //What line is the player sprite on?
var zmap = [];
var dz = 1;
var z = 0;
var TexOffset = 160;

//mousewheel
function MouseWheelHandler(e){
    // cross-browser wheel delta
    var e = window.event || e;

    if(e.wheelDelta>0)
    {
        delta = e.wheelDelta;
        ScrollSpeed = ScrollSpeed + delta / 1000;
    }
    else
    {
        delta = 0;    
    }
}

function drawGameStats()
{
     ctx.globalAlpha=1;
    //*************DRAW GAME SCENE***************************************		
    //Draw game stats   
    ctx.fillStyle = "#fff";
    ctx.font = 'bold 50px sans-serif';
    ctx.fillText(" Speed: "+Math.round(Speed*8)+ "Kmh", 150,240);
    
    ctx.fillStyle = "#fff";
    ctx.font = 'italic bold 30px sans-serif';
    ctx.fillText(" Time Left: "+TimeLeft+ "Sec.", 250,340);

    ctx.fillStyle = "#aaa";
    ctx.font = 'bold 80px sans-serif';
    ctx.fillText(" Distance: "+Math.round(Distance*100)/100+ "Km", 80,310);
}


function main(){

    //mousewheel
    document.addEventListener("mousewheel", MouseWheelHandler, false);
    // initial star setup
for (var i=0; i<units; i++)
{
   n = {};
   resetstar(n);
   stars.push(n);
}
    init();
    
    for(i=0;i<ROADLINES;i++)
    {
        var tmp;
        tmp = RoadY / (i - (ResY /2));
        zmap.push(tmp);
    }

    //Normalize ZMap so the line with the player on it is scale=1
    var b = 1 / zmap[PlrLine];
    b = b * 100;   //in percents because QBasic's MOD is lame

    //Zmap values: 1.01, 1.02, ... , 25, 50,100
    for(a=0;a<ROADLINES;a++)
    {
        zmap[a] = zmap[a] * b;
    }

    playerCar = new player(PLAYER_START_X,PLAYER_START_Y);

    //Start the game loop
    gameLoop = window.setInterval(draw, 20);  
}


//main loop
function draw(){
    
    //Clear screen
    reset();

    //*************CALCULATE DATA***************************************
    //Calculate data
    animationCounter++;
    Speed = ScrollSpeed;
    
    if((animationCounter%50) == 49)
    TimeLeft--;
	
	
    //Calculate average speed:
    SpeedSum = SpeedSum + Speed;    
    if((animationCounter%SPEED_CALC_INTERVAL) == 0)
    {	
            Speed = SpeedSum / SPEED_CALC_INTERVAL;
            Distance = Distance + (Speed*(100/3600000))*10;
            SpeedSum = 0;
    }
    
    //Friction
    if(ScrollSpeed >0)
    ScrollSpeed -= ScrollSpeed * (1/FRICTION_RATE);
	
	
    


    
    //Draw the road
    for(j=0;j<ROADLINES;j++)
    {
        if( ((zmap[j] + TexOffset) % TEXSIZE) > TEXSIZE/2) 
        {
            img = 0;
        }
        else
        {
            img = 1;
        }		
        drawRoad(img,ROADLINES-j,1);
    }


    TexOffset = TexOffset + ScrollSpeed;
	
    while(TexOffset >= TEXSIZE)
    {
        TexOffset = TexOffset - TEXSIZE;
    }
   
    playerCar.draw();
    
    ctx.globalAlpha=0.85;
    rf();
    
    drawGameStats();



    //************************GAMEOVER************************************
    if(TimeLeft == 0)
    {
        //alert("OVER");
        clearInterval(gameLoop);
        gameover = true;
        window.setInterval(rf,20);
    }

}





